import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/store/user'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    component: () => import('@/views/Layout.vue'),
    redirect: '/dashboard',
    meta: { requiresAuth: true },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/Dashboard.vue'),
        meta: { title: '仪表板', icon: 'DataAnalysis' }
      },
      {
        path: 'users',
        name: 'UserManage',
        component: () => import('@/views/user/UserList.vue'),
        meta: { title: '用户管理', icon: 'User' }
      },
      {
        path: 'users/:id',
        name: 'UserDetail',
        component: () => import('@/views/user/UserDetail.vue'),
        meta: { title: '用户详情', hidden: true }
      },
      {
        path: 'events',
        name: 'EventManage',
        component: () => import('@/views/event/EventList.vue'),
        meta: { title: '活动管理', icon: 'Calendar' }
      },
      {
        path: 'events/:id',
        name: 'EventDetail',
        component: () => import('@/views/event/EventDetail.vue'),
        meta: { title: '活动详情', hidden: true }
      },
      {
        path: 'clubs',
        name: 'ClubManage',
        component: () => import('@/views/club/ClubList.vue'),
        meta: { title: '俱乐部管理', icon: 'UserFilled' }
      },
      {
        path: 'clubs/:id',
        name: 'ClubDetail',
        component: () => import('@/views/club/ClubDetail.vue'),
        meta: { title: '俱乐部详情', hidden: true }
      },
      {
        path: 'venues',
        name: 'VenueManage',
        component: () => import('@/views/venue/VenueList.vue'),
        meta: { title: '场地管理', icon: 'Location' }
      },
      {
        path: 'venues/create',
        name: 'VenueCreate',
        component: () => import('@/views/venue/VenueForm.vue'),
        meta: { title: '创建场地', hidden: true }
      },
      {
        path: 'venues/edit/:id',
        name: 'VenueEdit',
        component: () => import('@/views/venue/VenueForm.vue'),
        meta: { title: '编辑场地', hidden: true }
      },
      {
        path: 'venues/:id',
        name: 'VenueDetail',
        component: () => import('@/views/venue/VenueDetail.vue'),
        meta: { title: '场地详情', hidden: true }
      },
      {
        path: 'statistics',
        name: 'Statistics',
        component: () => import('@/views/statistics/Overview.vue'),
        meta: { title: '数据统计', icon: 'TrendCharts' }
      },
      {
        path: 'system',
        name: 'System',
        meta: { title: '系统管理', icon: 'Setting' },
        redirect: '/system/admin-users',
        children: [
          {
            path: 'admin-users',
            name: 'AdminUserManage',
            component: () => import('@/views/system/AdminUserList.vue'),
            meta: { title: '后台用户管理', icon: 'Avatar' }
          },
          {
            path: 'user-roles',
            name: 'UserRoleManage',
            component: () => import('@/views/system/UserRoleManage.vue'),
            meta: { title: '用户角色分配', icon: 'UserFilled' }
          },
          {
            path: 'roles',
            name: 'RoleManage',
            component: () => import('@/views/system/RoleList.vue'),
            meta: { title: '角色管理', icon: 'Operation' }
          },
          {
            path: 'permissions',
            name: 'PermissionManage',
            component: () => import('@/views/system/PermissionList.vue'),
            meta: { title: '权限管理', icon: 'Lock' }
          }
        ]
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  const token = userStore.token

  if (to.meta.requiresAuth !== false && !token) {
    // 需要登录但未登录
    next('/login')
  } else if (to.path === '/login' && token) {
    // 已登录访问登录页，跳转到首页
    next('/')
  } else if (token && to.meta.requiresRole) {
    // 检查角色权限
    const userRoles = userStore.userInfo?.roles || []
    const hasRole = to.meta.requiresRole.some(role => userRoles.includes(role))
    
    if (!hasRole) {
      // 无权限，返回首页
      import('element-plus').then(({ ElMessage }) => {
        ElMessage.error('您没有权限访问该页面')
      })
      next('/dashboard')
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router

